package com.example.myblogserver.service.impl;

import com.example.myblogserver.common.context.BaseContext;
import com.example.myblogserver.common.exception.BaseException;
import com.example.myblogserver.dto.AddArticleDTO;
import com.example.myblogserver.dto.UpdateArticleDTO;
import com.example.myblogserver.mapper.CategoriesMapper;
import com.example.myblogserver.service.ArticleService;
import com.example.myblogserver.vo.CategoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import com.example.myblogserver.mapper.ArticlesMapper;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;
import com.example.myblogserver.model.Articles;

import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticlesMapper articlesMapper;
    @Autowired
    private CategoriesMapper categoriesMapper;
    @Override
    public void addArticle(AddArticleDTO addArticleDTO) {
        // 将 DTO 转换为实体类
        Articles article = new Articles();
        BeanUtils.copyProperties(addArticleDTO, article);
        article.setUserId(BaseContext.getCurrentUserId());
        article.setContentHtml(addArticleDTO.getContent());
        article.setMarkdownContent(addArticleDTO.getMarkdownContent().toString());
        article.setCreatedAt(new java.util.Date());
        // 插入数据库
        articlesMapper.insert(article);
    }

    @Override
    public List<Articles> getAllArticles() {
        List<Articles> articles = articlesMapper.selectAll();
        return articles;
    }

    @Override
    public List<Articles> getArticlesByCategoryId(Long categoryId) {
        List<Articles> articles = articlesMapper.getArticlesByCategoryId(categoryId);
        return articles;
    }

    @Override
    public Articles getArticleById(Long id) {
        Articles articles = articlesMapper.selectByPrimaryKey(id);
        return articles;
    }

    @Override
    public void deleteArticleById(Long id) {
        articlesMapper.deleteByPrimaryKey(id);
    }
    /**
     * 获取分类文章列表
     * @param categoryId 可选分类ID
     * @param pageSize 分页大小
     * @param currentPage 当前页码
     * @return
     */
    @Override
    public List<Articles> getArticlesByCategoryIdAndPage(Long categoryId, Integer pageSize, Integer currentPage) {
        List<Articles> articles = articlesMapper.getArticlesByCategoryIdAndPage(categoryId, pageSize, currentPage);

        return  articles;
    }
        @Override
    public void updateArticle(UpdateArticleDTO updateArticleDTO) {
        // 1. 校验文章是否存在
        Articles article = articlesMapper.selectByPrimaryKey(updateArticleDTO.getId());
        if (article == null) {
            throw new BaseException("文章不存在");
        }

        // 2. 更新文章信息
        BeanUtils.copyProperties( updateArticleDTO,article);
        article.setUserId(BaseContext.getCurrentUserId());
        article.setContentHtml(updateArticleDTO.getContent());
        article.setMarkdownContent(updateArticleDTO.getMarkdownContent().toString());
        article.setUpdatedAt(new java.util.Date());
        articlesMapper.updateByPrimaryKey(article);
    }

    @Override
    public List<CategoryVO> getArticleCategory() {
        List<CategoryVO> categoryVOList = categoriesMapper.selectAll().stream().map(category -> {
            CategoryVO categoryVO = new CategoryVO();
            categoryVO.setCategoryId(category.getId());
            categoryVO.setCategoryName(category.getName());
            return categoryVO;
        }).toList();
        return categoryVOList;
    }

}
